package com.yxmadoc.umlproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.yxmadoc.umlproject.config.enums.ReturnResult;
import com.yxmadoc.umlproject.model.RepairPrice;
import com.yxmadoc.umlproject.service.RepairPriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
public class RepairPriceController {

    @Autowired
    private RepairPriceService repairPriceService;

    @PostMapping("/repairPrice")
    public JSONObject repairPrice(@RequestBody RepairPrice repairPrice){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), repairPriceService.save(repairPrice));
        return data;
    }

    @GetMapping("/repairPrice/list/{repairerId}")
    public JSONObject listByRepairerId(@PathVariable("repairerId")String repairerId, @PageableDefault(sort = {"repairerId"}, direction = Sort.Direction.DESC)Pageable pageable){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), repairPriceService.findAllByRepairerId(repairerId, pageable));
        return data;
    }

    @GetMapping("/repairPrice/list/{orderId}")
    public JSONObject listByOrderId(@PathVariable("orderId")Integer orderId, @PageableDefault(sort = {"orderId"}, direction = Sort.Direction.DESC)Pageable pageable){
        JSONObject data = new JSONObject();
        data.put(ReturnResult.DATA.getCode(), repairPriceService.findAllByOrderId(orderId, pageable));
        return data;
    }

    @DeleteMapping("/repairPrice/{priceId}")
    public JSONObject delete(@PathVariable("priceId")Integer priceId){
        JSONObject result = new JSONObject();
        repairPriceService.delete(priceId);
        result.put(ReturnResult.STATUS.getCode(), true);
        return result;
    }

}
